package com.bootcamp.s2_0922.frontend._1005.Ex;

/**
 * @program: myClassCode
 * @description:
 * @author: Keyu Li
 * @create: 2021-10-05 21:41
 **/

public class ReverseLinkedList {
    public static void main(String[] args) {
        Node head = new Node(1, new Node(2, new Node(3, new Node(4, new Node(5, null)))));
        System.out.println(head);
        head = reverseList(head);
        System.out.println(head);
    }

    public static Node reverseList(Node p){
        if (p.next==null){
            return p;
        }
        Node head = reverseList(p.next);

        p.next.next = p;
        p.next =null;
        return head;
    }
}

class Node{
    int value;
    Node next;

    public Node(int value, Node next) {
        this.value = value;
        this.next = next;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("Node{");
        sb.append("value=").append(value);
        sb.append(", next=").append(next);
        sb.append('}');
        return sb.toString();
    }
}
